#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2025/4/24 19:25
# @Author   : MingTai
# @File     : image_to_video.py

import cv2
import os

from app.utils.time_util.time_handle import get_second_time_str


def images_to_video(image_folder, video_name, fps=2):
    images = [img for img in os.listdir(image_folder) if img.endswith(('.png', '.jpg', '.jpeg'))]
    images.sort()

    frame = cv2.imread(os.path.join(image_folder, images[0]))
    height, width, layers = frame.shape

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video = cv2.VideoWriter(video_name, fourcc, fps, (width, height))

    for image in images:
        try:
            video.write(cv2.imread(os.path.join(image_folder, image)))
        except Exception as e:
            print(f"Error processing {image}: {e}")

    cv2.destroyAllWindows()
    video.release()


# 将文件夹下的图片组合成一个视频
if __name__ == "__main__":
    image_folder = './extracted_frames/'  # 替换为实际的照片文件夹路径
    video_name = f'output_video{get_second_time_str()}.mp4'  # 输出视频的文件名
    fps = 2  # 视频帧率，可根据需要调整
    images_to_video(image_folder, video_name, fps)
